
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

/**
 * Represents a graph in the mathematical sense
 */
public class Graph {
    private AdjacencyMatrix _adjacencyMatrix; 
    private Edge _edgeArray[]; 
    private Boolean _debug = main.debug;
    private int _degreeArray[]; 
    private int _edgeCount;
    private int _vertexCount;

    /**
     * Default constructor, gets graph from a user-specified file
     */
    public Graph() throws FileNotFoundException
    {
        File graph;
        String file;
        Scanner sc, scf;
        int tmp=0, tmp2=0, edge_index=0;

        if(_debug) System.out.println("run graph constructor");

        sc = new Scanner(System.in);
        System.out.println("The name of a file to analyze. \"z\" for zachary (by default the file is looked for in the datasets folder)");
        file = sc.next();
        if (file.equals("z")) file = "zachary";
        file = "datasets/" + file;
        graph = new File(file);

        try 
        {
                scf = new Scanner(graph);

                _vertexCount = scf.nextInt();            //vertex count
                _edgeCount = scf.nextInt();           //edges count
                if(_debug) System.out.println("Read graph dimensions: " + _vertexCount + " verts " + _edgeCount + " edges");

                _adjacencyMatrix = new AdjacencyMatrix(_vertexCount);
                _edgeArray = new Edge[_edgeCount];
                
                while(scf.hasNextInt())
                {

                        //Indexing in file is assumed to start at 1, so we subtract 1 to make it start at 0
                        tmp = scf.nextInt() - 1;    //edge start
                        tmp2 = scf.nextInt() - 1;   //edge finish

                        _adjacencyMatrix.add(tmp, tmp2);
                        
                        _edgeArray[edge_index] = new Edge(tmp, tmp2);
                        edge_index++;
                        
                        //System.out.print(tmp + " ");
                        //System.out.println(tmp2);


                }

                if (_debug) _adjacencyMatrix.print();

                _degreeArray = _adjacencyMatrix.getDegArray();
                
                if (_debug)
                {
                    for(int p = 0; p< _degreeArray.length; p++)
                            System.out.print(_degreeArray[p] + " ");
                }
                
                System.out.println("The graph data has been processed successfuly. The graph has " + _vertexCount + " vertices and " + _edgeCount + " edges");

        }
        catch(FileNotFoundException e) 
        {
                System.out.println("File Not Found, terminating...");
                throw e;
        }

    }

    // Getters and setters
    
    /**
     * @return the number of edges in the graph
     */
    public int getEdgeCount()
    {
        return _edgeCount;
    }
    
    /**
     * @return the number of vertices in the graph
     */
    public int getVertexCount()
    {
        return _vertexCount;
    }
    
    /**
     * @return the adjacency matrix of the graph
     */
    public AdjacencyMatrix getAdjacencyMatrix()
    {
        return _adjacencyMatrix;
    }
    
    /**
     * @return an array of all the edges in the graph
     */
    public Edge[] getEdgeArray()
    {
        return _edgeArray;
    }
}
